#include <iostream>
#include <vector>
using namespace std;
int main()
{
	int n = 0, m = 0;
	string str1, str2;
	cin >> n >> m >> str1 >> str2;;
	vector<vector<int>> dp(n + 1, vector<int>(m + 1));
	str1 = " " + str1, str2 = " " + str2;
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			if (str1[i] == str2[j])
			{
				// dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
				dp[i][j] = dp[i - 1][j - 1] + 1;
			}
			else
			{
				dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
			}
		}
	}
	cout << dp[n][m];
	return 0;
}